<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
      <el-button v-if="in_right('Event/edit_test')" @click="open_test(row)" size="small">测试事件</el-button>
    </template>
  </list_url>

  <el-dialog v-model="edit_test.show" width="80%" :draggable="true" style="margin: auto;margin-top: 10vh;">
    <p style="margin-bottom: 15px;">【{{edit_test.title}}】事件测试：{{edit_test.info}}</p>
    <list_id v-model:value="edit_test.link_id" :config="edit_test.config"></list_id>
    <div class="flex" style="justify-content: right;">
      <el-button type="primary" @click="to_test">提交数据</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {iframe, in_right_admin} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import s94Mjs from "s94-js";
import {_config} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if (is_del){
      if(in_right_admin('Event/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('Event/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin('Event/add')) btns['添加'] = function(){ iframe('event/edit') }
      if(in_right_admin('Event/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }
    let event_config = [], function_map = {};
    ajax({url:'/admin/event/function_config',cache:true,async:false},function (res){
      event_config = res.data.event_map;
      function_map = res.data.function_map;
      return false;
    })
    let forms_config = {
      event_name: {label:'触发事件',name:'where[event_name][eq]',type:'select_one',config:event_config},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      model_alias: {name:'model_alias', label:'模型', width:'100px'},
      event_name_alias: {name:'event_name_alias', label:'触发事件', type:'text_one', width:'100px'},
      throw: {name:'throw', label:'抛出异常', type:'radio_switch', width:'100px'},
      condition_info: {name:'condition_info', label:'执行详情', type:'text_more'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    if (!this.admin_config('this_tenant.id')){
      list_cols['tenant_id'] = {name:'tenant_id', label:'租户', width:'65px', fixed:'right'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'180px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/event/'+(is_del?'recycle':'list'),
        title: "事件列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          s94Mjs.each(list, function (row){
            row.event_name_alias = event_config[row.event_name] || '未知';
            row.condition_info = app.condition_info(row);
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
      function_map: function_map,
      edit_test: {show: false, link_id:'', config:''}
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('Event/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('event/edit?'+s94Mjs.param_encode(param))
    },
    condition_info(condition){
      if (!this.function_map[condition.function]) return '';
      let fuc = this.function_map[condition.function];
      let param = JSON.parse(condition.param);
      let data = fuc.info_param || [];
      fuc.param.forEach(function(k,i){
        data[k] = param[i];
      })
      return vue_template(fuc.info, data);
    },
    open_test(row){
      if (row.event_name.indexOf('Kickback') === 0){
        this.edit_test.config = '/user_kickback/index';
      }
      if (row.event_name.indexOf('ModelData') === 0 && row.model_name){
        this.edit_test.config = '/model_base/index?model_name='+row.model_name+'&model_alias='+row.model_alias;
      }
      if (row.event_name.indexOf('Order') === 0 && row.model_name){
        this.edit_test.config = '/order/index?model_name='+row.model_name+'&model_alias='+row.model_alias;
      }
      if (row.event_name.indexOf('User') === 0){
        this.edit_test.config = '/user/index';
      }
      if (row.event_name.indexOf('Withdraw') === 0){
        this.edit_test.config = '/user_withdraw/index';
      }
      this.edit_test.title = row.event_name_alias;
      this.edit_test.info = row.condition_info;
      this.edit_test.show = true;
      this.edit_test.id = row.id;
      this.edit_test.link_id = '';
    },
    to_test(){
      ajax({url:'/admin/event/edit_test', data:this.edit_test})
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;


  }
}
</script>
